//
//  ZAssignMeetingVC.m
//  WeClubs
//
//  Created by Zed Link on 17/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZAssignMeetingVC.h"
#import "ZTodoMeetingCell.h"
#import "ZTodoFlowLayout.h"
#import "ZNewMeetingVC.h"
#import "ZMeetingModel.h"

NSString * const AssignMeetingCellReuseID = @"ZAssignMeetingCellReuseID";

@interface ZAssignMeetingVC () <UICollectionViewDelegate, UICollectionViewDataSource>

/** 待办会议列表 */
@property (weak, nonatomic) UICollectionView *meetingView;

/** 待办会议数据源 */
@property (strong, nonatomic) NSArray <ZTodoMeetingModel *> *meetingModels;

//会议数据源
@property(nonatomic)NSMutableArray *meetingDataArr;

@end

@implementation ZAssignMeetingVC

#pragma mark - View Life Cycle
- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    [self setupNav];
    
    [self setupCollectionInit];
    
    [self loadDataFormNetWork];
    
}
#pragma mark - setupUI
- (void)setupNav{
    [super zConfigNavigation];
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.title = @"会议管理";
    self.navigationItem.rightBarButtonItem = [self zNavItemWithTitle:@"发起会议" andSelector:@selector(createMissionAction)];
}
- (void)setupCollectionInit{
    // ========== 待办会议列表 ==========
    ZTodoFlowLayout *layout = [[ZTodoFlowLayout alloc] init];
    
    UICollectionView *meetingView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    meetingView.delegate = self;
    meetingView.dataSource = self;
    
    [meetingView setBackgroundColor:GENERAL_VIEW_BG_COLOR];
    [meetingView registerNib:[UINib nibWithNibName:@"ZTodoMeetingCell" bundle:nil] forCellWithReuseIdentifier:AssignMeetingCellReuseID];
    meetingView.contentInset = UIEdgeInsetsMake(DYNAMIC_TOP_SPACE, 0, DYNAMIC_TOP_SPACE, 0);
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.meetingView = meetingView;
    [self.view addSubview:meetingView];
    [self.meetingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(64, 0, 0, 0));
    }];
}

#pragma mark - loadData
-(void)loadDataFormNetWork{

    [MBProgressHUD zAlertProgress:@"正在加载中"];

    [HTTPRequest POST:kGet_my_meetingUrl parameter:@{@"sponsor_id":@([ZUserInfo sharedInstance].userId)} success:^(id resposeObject) {
        [MBProgressHUD zAlertHideHUD];
        
        if ([resposeObject[@"result_code"] integerValue] == 2000) {
            
            for (NSDictionary *dic in resposeObject[@"data"][@"meeting"]) {
                
                ZMeetingModel * model = [[ZMeetingModel alloc]initWithDictionary:dic];

                [self.meetingDataArr addObject:model];
            }
            [self.meetingView reloadData];
            
        }else{
            [MBProgressHUD zAlertMessage:resposeObject[@"result_msg"] to:self.view afterDelay:1.0];
            
        }
    } failure:^(NSError *error) {
        
        [MBProgressHUD zAlertHideHUD];
    }];


}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.meetingDataArr.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ZTodoMeetingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:AssignMeetingCellReuseID forIndexPath:indexPath];
    cell.type = ZMeetingTypeAssign;
    cell.meetingModel = self.meetingDataArr[indexPath.row];
    return cell;
}

#pragma mark - SEL Methods
- (void)createMissionAction{
    ZNewMeetingVC *newVC = [ZNewMeetingVC zNew];
    [self.navigationController pushViewController:newVC animated:YES];
}

#pragma mark - Setter && Getter Methods
- (NSMutableArray *)meetingDataArr{

    if (!_meetingDataArr) {
        _meetingDataArr = [NSMutableArray array];
    }

    return _meetingDataArr;
}
@end
